/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.command;

import java.awt.geom.Point2D;
import java.util.StringTokenizer;

import javax.swing.JInternalFrame;

import br.ufmg.dees.insane.commons.command.Command;
import br.ufmg.dees.insane.commons.controller.Controller;
import br.ufmg.dees.insane.commons.view.DrawingArea;
import br.ufmg.dees.insane.model.geo.GControlPoint;
import br.ufmg.dees.insane.model.geo.GCubicCurve;
import br.ufmg.dees.insane.model.geo.GPoint;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;
import br.ufmg.dees.insane.ui.rich.full.controller.GeometricController;

/** The AddCubicKeyb command.
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @since February 2004
 */
public class AddCubicKeybCommand implements Command, Cloneable {

	private InsaneInternalInterface parent;
	private Controller controller;
	private DrawingArea da;
	private AddCubicMouseCommand cmd;
	private StringTokenizer stk;
	
//*****************************************************************************
	
	/** Constructor method. */
	public AddCubicKeybCommand(JInternalFrame parent) {
		this.parent = (InsaneInternalInterface)parent;
		controller = this.parent.getController();
		da = this.parent.getDrawing();
		cmd = (AddCubicMouseCommand) this.parent.getDrawing().getClickedCommand();
	}
	
//*****************************************************************************
	
	/** Execute method. */
public void execute() {
		
		String str = parent.getConsole().getLastCommand();
		double x0 = 0;
		double y0 = 0;
		
		if (str.startsWith("@")) {
			String str2 = str.substring(1);
			stk = new StringTokenizer(str2, ", ");
			Point2D ip = parent.getConsole().getLastPoint();
			x0 = ip.getX();
			y0 = ip.getY();
		} else {
			stk = new StringTokenizer(str, ", ");
		}
		
		try {
			Point2D po = new Point2D.Double(x0+Double.parseDouble(stk.nextToken()), y0+Double.parseDouble(stk.nextToken()));
			
			cmd.setClicks(cmd.getClicks() + 1 );
			
			if (cmd.getClicks() % 4 == 1){
				if (!controller.contains(po)){
					GPoint p = new GPoint(po);
					controller.add(p);
					da.repaint();
					cmd.addNewPoint((GPoint)controller.get(po));
					cmd.setInitialPoint((GPoint)controller.get(po));
					cmd.addNewPoint(cmd.getInitialPoint());
					parent.addOutputText(parent.getResourceBundle().getString("cmIniPointCreated") + " " + cmd.getInitialPoint().getX() + ", " + cmd.getInitialPoint().getY());
				}else{
					cmd.setInitialPoint((GPoint)controller.get(po));
					parent.addOutputText(parent.getResourceBundle().getString("cmIniPointChosen") + " " + cmd.getInitialPoint().getX() + ", " + cmd.getInitialPoint().getY());
				}
				PreviewLineCommand pl = new PreviewLineCommand(parent);
				pl.setInitialPoint(da.getViewPoint(cmd.getInitialPoint()));
				da.setMovedCommand(pl);
				da.setRepainting(true);
				da.repaint();
				parent.getConsole().setLastPoint(new Point2D.Double(po.getX(), po.getY()));
				parent.addOutputText(parent.getResourceBundle().getString("cmSpecFirstControlPoint"));
			}else if (cmd.getClicks() % 4 == 2){
				if (!((GeometricController)controller).contains(po)){
					GControlPoint p = new GControlPoint(po);
					controller.add(p);
					parent.addOutputText(parent.getResourceBundle().getString("cmControlPointCreated") + " "+ p.getX() + ", " + p.getY());
					da.repaint();
					cmd.setControlPoint1((GPoint) controller.get(po));
					cmd.addNewCtrlPoint(cmd.getControlPoint1());
					parent.addOutputText(parent.getResourceBundle().getString("cmFirstControlPointCreated") + " " + cmd.getControlPoint1().getX() + ", " + cmd.getControlPoint1().getY());
				}else{
					cmd.setControlPoint1((GPoint)controller.get(po));
					parent.addOutputText(parent.getResourceBundle().getString("cmFirstControlPointChosen") + " " + cmd.getControlPoint1().getX() + ", " + cmd.getControlPoint1().getY());
				}
				PreviewQuadCommand pq = new PreviewQuadCommand(parent);
				pq.setInitialPoint(da.getViewPoint(cmd.getInitialPoint()));
				pq.setCtrlPoint(da.getViewPoint(cmd.getControlPoint1()));
				da.setMovedCommand(pq);
				da.setRepainting(true);
				da.repaint();
				parent.getConsole().setLastPoint(new Point2D.Double(po.getX(), po.getY()));
				parent.addOutputText(parent.getResourceBundle().getString("cmSpecSecondControlPoint"));
			}else if (cmd.getClicks() % 4 == 3){
				if (!((GeometricController)controller).contains(po)){
					GControlPoint p = new GControlPoint(po);
					controller.add(p);
					parent.addOutputText(parent.getResourceBundle().getString("cmControlPointCreated") + " "+ p.getX() + ", " + p.getY());
					da.repaint();
					cmd.setControlPoint2((GPoint)controller.get(po));
					cmd.addNewCtrlPoint(cmd.getControlPoint2());
					parent.addOutputText(parent.getResourceBundle().getString("cmSecondControlPointCreated") + " " + cmd.getControlPoint2().getX() + ", " + cmd.getControlPoint2().getY());
				}else{
					cmd.setControlPoint2((GPoint)controller.get(po));
					parent.addOutputText(parent.getResourceBundle().getString("cmSecondControlPointChosen") + " " + cmd.getControlPoint2().getX() + ", " + cmd.getControlPoint2().getY());
				}
				PreviewCubicCommand pcb = new PreviewCubicCommand(parent);
				pcb.setInitialPoint(da.getViewPoint(cmd.getInitialPoint()));
				pcb.setCtrlPoint1(da.getViewPoint(cmd.getControlPoint1()));
				pcb.setCtrlPoint2(da.getViewPoint(cmd.getControlPoint2()));
				da.setMovedCommand(pcb);
				da.setRepainting(true);
				da.repaint();
				parent.getConsole().setLastPoint(new Point2D.Double(po.getX(), po.getY()));
				parent.addOutputText(parent.getResourceBundle().getString("cmSpecFinalPoint"));
			}else{
				if(po.getX()!=cmd.getInitialPoint().getX() || po.getY()!=cmd.getInitialPoint().getY()) {
					if (!controller.contains(po)){
						GPoint p = new GPoint(po);
						controller.add(p);
						da.repaint();
						cmd.addNewPoint((GPoint)controller.get(po));
						cmd.setFinalPoint((GPoint)controller.get(po));
						cmd.addNewPoint(cmd.getFinalPoint());
						parent.addOutputText(parent.getResourceBundle().getString("cmFinalPointCreated") + " " + cmd.getFinalPoint().getX() + ", " + cmd.getFinalPoint().getY());
					}else{
						cmd.setFinalPoint((GPoint)controller.get(po));
						parent.addOutputText(parent.getResourceBundle().getString("cmFinalPointChosen") + " " + cmd.getFinalPoint().getX() + ", " + cmd.getFinalPoint().getY());
					}
					da.setMovedCommand(null);
					da.setRepainting(false);
					da.setShape(null);
					cmd.setCurve(new GCubicCurve(cmd.getInitialPoint(), cmd.getControlPoint1(), cmd.getControlPoint2(), cmd.getFinalPoint()));
					controller.add(cmd.getCurve());
					da.repaint();
					parent.getConsole().setLastPoint(new Point2D.Double(po.getX(), po.getY()));
					parent.addToUndo((Command)cmd.clone());
					cmd.clearNewCtrlPoints();
					cmd.clearNewPoints();
					parent.addOutputText(parent.getResourceBundle().getString("cmSpecifyIniPoint"));
				} else {
					parent.addOutputText(parent.getResourceBundle().getString("cmMustBeDifferent"));
					parent.addOutputText(parent.getResourceBundle().getString("cmSpecFinalPoint"));
					cmd.setClicks(cmd.getClicks() - 1 );
				}
			}
		}catch (Exception e) {
				parent.addOutputText(parent.getResourceBundle().getString("cmInvalidData"));
		}
	}
	
//*****************************************************************************
	
	/** Impossible to undo - Not implemented. */
	public void undo() {}
	
//*****************************************************************************
	
	/** Impossible to redo - Not implemented. */
	public void redo() {}
	
//*****************************************************************************
}
